package cn.com.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

/**
 * 合并对象
 *
 * @author LiuGuodong
 * @date 2023/12/22
 */
public class MergeUtil {
    /**
     * 选项不允许覆盖
     */
    private static final CopyOptions OPTIONS = CopyOptions.create().setIgnoreNullValue(true).setOverride(false);
    /**
     * 选项允许覆盖
     */
    private static final CopyOptions OPTIONS_ALLOW_OVERRIDE = CopyOptions.create().setIgnoreNullValue(true).setOverride(true);

    /**
     * 将sourceBean中的属性合并到targetBean，忽略Null值，非Null值不允许覆盖
     *
     * @param sourceBean 源bean
     * @param targetBean 目标bean
     * @return {@link Object}
     */
    public static <T> T merge(Object sourceBean, T targetBean) {
        BeanUtil.copyProperties(sourceBean, targetBean, OPTIONS);

        return targetBean;
    }

    /**
     * 将sourceBean中的属性合并到targetBean，忽略Null值，非Null值允许覆盖
     *
     * @param sourceBean 源bean
     * @param targetBean 目标bean
     * @return {@link Object}
     */
    public static <T> T mergeAllowOverride(Object sourceBean, T targetBean) {
        BeanUtil.copyProperties(sourceBean, targetBean, OPTIONS_ALLOW_OVERRIDE);

        return targetBean;
    }

}
